package com.GainLoop;


import java.io.InputStream;
import java.net.URL;
import java.util.Date;

import org.alexd.jsonrpc.JSONRPCException;
import org.json.JSONException;

import android.app.Activity;
import android.app.SearchManager;

import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;

import com.GainLoop.R;
import com.GainLoop.Adapters.OffersListAdapter;
import com.GainLoop.Api.ApiResponseObject;
import com.GainLoop.Api.Users;
import com.GainLoop.Api.Entity.User;
import com.GainLoop.Data.Registry;

public class UserPage extends Activity  {
	
	final static String USER_ID = "user id";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_page);
		createPage();
	}
	private void createPage(){
		Bundle extras = getIntent().getExtras();
		Long userId;
		User user;
		if(extras!=null && (userId = extras.getLong(USER_ID))!=null ){ //if we have a user ID given.
			user = getUser(userId);
			Log.v("user=","diffrenet us");
		}
		else{ // else assume it's the current app user.
			user=Registry.GetUser();
			Log.v("user=","current user");
		}
		
		if (user==null){
			return; //TODO: need to handle this error.
		}
		TextView userDetails=(TextView)findViewById(R.id.user_details);
		TextView fbLink=(TextView)findViewById(R.id.link_fb);
		TextView location=(TextView)findViewById(R.id.user_location);
		TextView email=(TextView)findViewById(R.id.user_email);
		
		userDetails.setText(getDetailsToString(user));
		
		//ImageView img=(ImageView)findViewById(R.id.user_img);
		
		//Adding a link to FB profile page of the User
		Long userFbId = Registry.GetUser().getFbId();
		fbLink.setText(Html.fromHtml("<a href=http://www.facebook.com/profile.php?id="+userFbId+">Facebook Profile</a> "));
		fbLink.setMovementMethod(LinkMovementMethod.getInstance());
		
		Object content = null;
	    try{
	      URL url = new URL("http://graph.facebook.com/"+user.getFbId()+"/picture?type=normal");
	      content = url.getContent();
	    }
	      catch(Exception ex)
	    {
	        ex.printStackTrace();
	    }
	    InputStream is = (InputStream)content;
	    Drawable image = Drawable.createFromStream(is, "src");
	    //img.setImageDrawable(image);
	    userDetails.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null);
	    userDetails.setCompoundDrawablePadding(100);
		location.setText(user.getCountry());
		email.setText(user.getEmail());
		
		
	}
	private String getDetailsToString(User user) {
		StringBuffer str = new StringBuffer();
		String tempStr;
		str.append(user.getName());
		str.append("\n");
		
		tempStr = user.getLocationName();
		if(tempStr!=null && tempStr.length()>0){
			str.append("Location: "+user.getLocationName()+"\n");
		}
		
		Date dateArrived = user.getCreateTime();
		if(dateArrived!=null){
			str.append("Joined: "+dateArrived.getYear()+"\n");
		}
		return str.toString();
	}
	private User getUser(Long userId) {
		Users users = new Users();
		ApiResponseObject<User> resp = null;
		try {
			resp = users.getById(userId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONRPCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		User user = null;
		user=resp.getData();
		return user;
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
    	Log.v("home", "inflating menu ..");
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        menu.setGroupVisible(R.id.group1, false);
        menu.setGroupVisible(R.id.group2, false);
        menu.setGroupVisible(R.id.group3, false);
        
        menu.setGroupVisible(R.id.groupProfile, false);
        
        Log.v("home", "disable menus ..");
        
        return true;
    }
    public boolean onOptionsItemSelected (MenuItem item){
    	Toast toast;
    	Intent intent;
		 switch (item.getItemId()) {
		    case R.id.menu_profile_button:
		    	intent=new Intent(UserPage.this,UserPage.class);
		    	UserPage.this.startActivity(intent);
		        return true;
		    case R.id.help_button:
		    	toast = Toast.makeText(UserPage.this,"help", Toast.LENGTH_LONG);
				toast.show();
		        return true; 
		    case R.id.menu_search_button:
		    	intent=new Intent(UserPage.this,GainSearch.class);
		    	UserPage.this.startActivity(intent);
		        return true;
		    case R.id.menu_new_gain_button:
		    	intent=new Intent(UserPage.this,AddRequest.class);
		    	UserPage.this.startActivity(intent);
		        return true;   
		    case R.id.menu_home_button:
		    	intent=new Intent(UserPage.this,Home.class);
		    	UserPage.this.startActivity(intent);
		        return true; 
		        
		    default:
		        return super.onOptionsItemSelected(item);
		    }
		
    }

}

